#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
char b[7][7];
int x, y;
string cmd;

bool read_pazzle() {
  string s;
  rep(i, 1, 5) {
    getline(cin, s);
    if (s.empty()) getline(cin, s);  // skip empty line
    if (s == "Z") return false;      // end of game
    rep(j, 1, 5) b[i][j] = (s[j - 1] ? s[j - 1] : ' ');
  }
  rep(i, 1, 5) rep(j, 1, 5) {
    if (b[i][j] == ' ') x = i, y = j;
  }
  return true;
}
bool mov(char op) {
  if (op == 'A') {
    if (x == 1) return false;
    swap(b[x][y], b[x - 1][y]);
    x--;
  } else if (op == 'B') {
    if (x == 5) return false;
    swap(b[x][y], b[x + 1][y]);
    x++;
  } else if (op == 'L') {
    if (y == 1) return false;
    swap(b[x][y], b[x][y - 1]);
    y--;
  } else if (op == 'R') {
    if (y == 5) return false;
    swap(b[x][y], b[x][y + 1]);
    y++;
  } else
    return false;
  return true;
}
bool action() {
  string cmd;
  while (true) {
    char c;
    cin >> c;
    if (c == '0') break;
    cmd.push_back(c);
  }
  for (char c : cmd) {
    if (!mov(c)) return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int id = 0;
  while (read_pazzle()) {
    id++;
    if (id > 1) cout << endl;
    cout << "Puzzle #" << id << ":\n";
    if (action()) {
      rep(i, 1, 5) rep(j, 1, 5) cout << b[i][j] << " \n"[j == 5];
    } else {
      cout << "This puzzle has no final configuration.\n";
    }
  }
  return 0;
}